package com.bqtv.sport.commen.widget

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.annotation.RawRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.bqtv.sport.databinding.WidgetStatusLayoutBinding

class StatusLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    lateinit var binding: WidgetStatusLayoutBinding

    /** 主布局  */
    private var mMainLayout: ViewGroup? = null

    /** 重试监听  */
    private var mListener: OnRetryListener? = null

    /**
     * 显示
     */
    fun show() {
        if (mMainLayout == null) {
            //初始化布局
            initLayout()
        }
        if (isShow()) {
            return
        }
        // 显示布局
        mMainLayout!!.visibility = VISIBLE
    }

    /**
     * 隐藏
     */
    fun hide() {
        if (mMainLayout == null || !isShow()) {
            return
        }
        //隐藏布局
        mMainLayout!!.visibility = INVISIBLE
    }

    /**
     * 是否显示了
     */
    fun isShow(): Boolean {
        return mMainLayout != null && mMainLayout!!.visibility == VISIBLE
    }

    /**
     * 设置提示图标，请在show方法之后调用
     */
    fun setIcon(@DrawableRes id: Int) {
        setIcon(ContextCompat.getDrawable(context, id))
    }

    fun setIcon(drawable: Drawable?) {
        binding.ivStatusIcon.apply {
            if (isAnimating) {
                cancelAnimation()
            }
            setImageDrawable(drawable)
        }
    }

    /**
     * 设置提示动画
     */
    fun setAnimResource(@RawRes id: Int) {
        binding.ivStatusIcon.apply {
            setAnimation(id)
            if (!isAnimating) {
                playAnimation()
            }
        }
    }

    /**
     * 设置提示文本，请在show方法之后调用
     */
    fun setHint(@StringRes id: Int) {
        setHint(resources.getString(id))
    }

    fun setHint(text: CharSequence?) {
        binding.ivStatusText.text = text
    }

    /**
     * 初始化提示的布局
     */
    private fun initLayout() {
        binding = WidgetStatusLayoutBinding.inflate(LayoutInflater.from(context))
        mMainLayout = binding.root
        if (mMainLayout!!.background == null) {
            // 默认使用 windowBackground 作为背景
            val typedArray =
                context.obtainStyledAttributes(intArrayOf(android.R.attr.windowBackground))
            mMainLayout!!.background = typedArray.getDrawable(0)
            mMainLayout!!.isClickable = true
            typedArray.recycle()
        }
        binding.ivStatusIcon.setOnClickListener(mClickWrapper)
        addView(mMainLayout)
    }

    /**
     * 设置重试监听器
     */
    fun setOnRetryListener(listener: OnRetryListener?) {
        mListener = listener
    }

    /**
     * 点击事件包装类
     */

    private var mClickWrapper: OnClickListener = OnClickListener {
        mListener?.onRetry(this@StatusLayout)
    }

    /**
     * 重试监听器
     */
    interface OnRetryListener {
        /**
         * 点击了重试
         */
        fun onRetry(layout: StatusLayout)
    }
}